package com.template.utlis;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author wangqi
 * @date 2024-01-17 16:04
 */
public class StringCommons {
    public static final String PLACEHOLDER = "\\{\\}";

    public static final String EMPTY = "";

    public static final String TAG = "tag";

    public static final String CONTENT = "content";

    public static final String TEXT_ALIGN = "text_align";

    public static final String FLEX_MODE = "flex_mode";

    public static final String BACKGROUND_STYLE = "background_style";

    public static final String HORIZONTAL_SPACING = "horizontal_spacing";

    public static final String SMALL = "small";

    public static final String COLUMNS = "columns";

    public static final String WIDTH = "width";

    public static final String WEIGHT = "weight";

    public static final String VERTICAL_ALIGN = "vertical_align";

    public static final String ELEMENTS = "elements";

    public static final String AUTO = "auto";

    public static final String WEIGHTED = "weighted";

    public static final String TEMPLATE = "template";

    public static final String TITLE = "title";

    public  static  final String HEADER="header";

    public static final String PLAIN_TEXT = "plain_text";

    private static final Map<String, String> translateMap = new HashMap<>();

    public static String buildString(String msg, Object... placeHolders){
        for (Object placeHolder : placeHolders) {
            msg = msg.replaceFirst(PLACEHOLDER, placeHolder.toString());
        }
        for (Object placeHolder : placeHolders) {
            if (Objects.nonNull(placeHolder)&&placeHolder instanceof Exception){
                Exception exception = (Exception) placeHolder;
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                exception.printStackTrace(pw);
                msg=msg+"\n"+ sw;
            }
        }
        return msg;
    }

    static {
        translateMap.put(" ", "&emsp;");
        translateMap.put(">", "&#62;");
        translateMap.put("<", "&#60;");
        translateMap.put("~", "&sim;");
        translateMap.put("-", "&#45;");
        translateMap.put("!", "&#33;");
        translateMap.put("*", "&#42;");
        translateMap.put("/", "&#47;");
        translateMap.put("\\", "&#92;");
        translateMap.put("[", "&#91;");
        translateMap.put("]", "&#93;");
        translateMap.put("(", "&#40;");
        translateMap.put(")", "&#41;");
        translateMap.put("#", "&#35;");
        translateMap.put(":", "&#58;");
        translateMap.put("+", "&#43;");
        translateMap.put("\"", "&#34;");
        translateMap.put("'", "&#39;");
        translateMap.put("`", "&#96;");
        translateMap.put("$", "&#36;");
        translateMap.put("_", "&#95;");
        translateMap.put("-", "&#45;");

    }

    public static String translateContent(String content) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < content.length(); i++) {
            char c = content.charAt(i);
            String str = translateMap.get(String.valueOf(c));
            stringBuilder.append(str == null ? c : str);
        }
        return stringBuilder.toString();
    }

    public enum TemplateEnum {
        TURQUOISE("turquoise"),
        ORANGE("orange"),
        RED("red"),
        YELLOW("yellow"),
        GREEN("green"),
        AQUAMARINE("wathet"),
        VIOLET("violet"),
        PURPLE("purple"),
        INDIGO("indigo"),
        GREY("grey"),
        DEFAULT("default"),
        CRIMSON("carmine"),
        BLUE("blue");
        private String value;

        TemplateEnum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    public enum TagEnum {
        PLAIN_TEXT("plain_text",""),
        MARKDOWN("markdown", ""),
        COLUMN_SET("column_set", ""),
        COLUMN("column", "");


        private String value;
        private String desc;

        TagEnum(String value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        public String getValue() {
            return value;
        }
    }

    public enum FontColorEnum {
        RED("<font color='red'>", " </font>"),
        GREY("<font color='grey'>", " </font>"),
        GREEN("<font color='green'>", " </font>");
        private String prefix;
        private String suffix;

        FontColorEnum(String prefix, String suffix) {
            this.prefix = prefix;
            this.suffix = suffix;
        }

        public String getPrefix() {
            return prefix;
        }

        public String getSuffix() {
            return suffix;
        }
    }

    public enum FlexModeEnum {
        TRISECT("trisect", "三列等分布局"),
        BISECT("bisect", "两列等分布局"),
        FLOW("flow", "列流式排布（自动换行），当一行展示不下一列时，自动换至下一行展示"),
        STRETCH("stretch", "列布局变为行布局，且每列（行）宽度强制拉伸为 100%，所有列自适应为上下堆叠排布"),
        NONE("none", "不做布局上的自适应，在窄屏幕下按比例压缩列宽度", "默认值");
        private String value;
        private String desc;
        private String defaultValueDesc;

        FlexModeEnum(String value, String desc, String defaultValueDesc) {
            this.value = value;
            this.desc = desc;
            this.defaultValueDesc = defaultValueDesc;
        }

        FlexModeEnum(String value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        public String getValue() {
            return value;
        }

    }

    public enum TextAlignEnum {
        LEFT("left"),
        CENTER("center"),
        RIGHT("right");
        private String value;

        TextAlignEnum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    public enum VerticalAlignEnum {
        TOP("top", "顶对齐"),
        CENTER("center", "居中对齐"),
        BOTTOM("bottom", "底部对齐");
        private String value;
        private String desc;

        VerticalAlignEnum(String value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        public String getValue() {
            return value;
        }
    }

    public enum BackgroundStyleEnum {
        DEFAULT("default"),
        GREY("grey"),
        DARK_MODE("dark mode");
        private String value;

        public String getValue() {
            return value;
        }

        BackgroundStyleEnum(String value) {
            this.value = value;
        }
    }

    public enum WeightEnum {
        ONE(1),
        TWO(2),
        THREE(3),
        FOUR(4),
        FIVE(5);
        private Integer value;

        WeightEnum(Integer value) {
            this.value = value;
        }

        public Integer getValue() {
            return value;
        }
    }

}
